﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.SystemSetting
{
    public partial class FormPermissionGroup : Base.FormBaseToolBar
    {
        public FormPermissionGroup()
        {
            InitializeComponent(); 
        }
        protected override void FormAdd()
        {
            var formAdd = new FormPermissionGroupEdit(null);
            var res = formAdd.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        protected override void FormEdit()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            var form = new FormPermissionGroupEdit(dr);
            var res = form.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks > 1)
            {
                FormEdit();
            }
        }

        private void FormPermissionGroup_Load(object sender, EventArgs e)
        {
            FormRefresh();
        }
        public override void FormRefresh()
        {
            var dt = new BAL.BAL_SystemSetting().GetAllPermissionGroup();
            this.gridControl1.DataSource = dt;
        }
        /// <summary>
        /// 权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barbtnPermission_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowPermission(false);
        }

        private void ShowPermission(bool isUser)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null || dr["GroupID"] == DBNull.Value)
                return;
            var groupId = Convert.ToInt32(dr["GroupID"]);
            var form = new FormPermission(groupId, dr["GroupName"] == DBNull.Value ? string.Empty : dr["GroupName"].ToString(), isUser);
            form.ShowDialog();
        }
        /// <summary>
        /// 成员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barbtnUsers_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowPermission(true);
        }
        protected override void FormDelete()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null || dr["GroupID"] == DBNull.Value)
                return;
            if (UI.ShowWarningOKCancel(
                string.Format("删除权限组将会该组设置的所有权限\r\n确定删除权限组[{0}]", dr["GroupName"]),
                "权限组删除提示(慎重操作)"
                ) != DialogResult.OK)
                return;
            var groupId = Convert.ToInt32(dr["GroupID"]);
            var isOK = new BAL.BAL_SystemSetting().DeltePermissionGroup(groupId);
            if (isOK)
            {
                UI.ShowInfo("删除成功");
                FormRefresh();
            }
            else
            {
                UI.ShowError("删除失败");
            }
        }
    }
}
